package cn.com.fuhuitong.main.cate.ui.fragment

import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.entity.EmptyState
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeFragment
import cn.com.fuhuitong.main.cate.adapter.CVT
import cn.com.fuhuitong.main.cate.adapter.CategoryGroupAdapter
import cn.com.fuhuitong.main.cate.vm.CategoryViewModel
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.main.cate.entity.CateGroupEntity
import kotlinx.android.synthetic.main.fragment_main_category.*


class CategoryFragment : ViewModeFragment<CategoryViewModel>() {


    private lateinit var categoryGroupAdapter: CategoryGroupAdapter

    override fun viewModel(): CategoryViewModel = ViewModelProvider(
        this, ViewModelProvider.AndroidViewModelFactory(BaseApplication.INSTANCE)
    ).get(CategoryViewModel::class.java)

    override val layoutResId: Int
        get() = R.layout.fragment_main_category

    override fun initView() {
        this.initViewRecyclerAdapter(recycler_category, arrayOf(CVT.CATEGORY_GROUP.ordinal))

        delegateAdapter.addAdapter(CategoryGroupAdapter(
            requireContext(), LinearLayoutHelper().apply {
                setDividerHeight(requireActivity().resources.getDimensionPixelSize(R.dimen.dp_2))
            }
        ).apply {
            categoryGroupAdapter = this
        })

        categoryGroupAdapter.setOnItemClickListener { _, i, _ ->
            categoryGroupAdapter.selectedPosition = i
            view_pager_category.setCurrentItem(i, false)
        }
    }

    override fun initViewMode() {

        viewModel.cateGroupDataLiveData.observe(viewLifecycleOwner, Observer { response ->
            handlerResponseEmpty(
                status = response.status, message = response.statusTip,
                length = response.response?.data?.size ?: 0
            )
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                fillData(it)
            }
        })
    }

    override fun initData() {
        viewModel.cateGroup()
    }

    override fun onLoadingEmptyView(state: EmptyState) {
        super.onLoadingEmptyView(state)
        viewModel.cateGroup()
    }

    private fun fillData(cateGroups: MutableList<CateGroupEntity>) {

        val linearParams: ViewGroup.LayoutParams = recycler_category.layoutParams
        linearParams.width =
            requireActivity().resources.getDimensionPixelSize(R.dimen.dp_100)
        recycler_category.layoutParams = linearParams

        categoryGroupAdapter.setAddDataNew(cateGroups)

        view_pager_category.offscreenPageLimit = cateGroups.size
        with(view_pager_category) {
            getChildAt(0)?.overScrollMode = View.OVER_SCROLL_NEVER
            adapter = object : FragmentStateAdapter(this@CategoryFragment) {

                override fun getItemCount(): Int = cateGroups.size

                override fun createFragment(position: Int): Fragment {
                    return CategoryChildFragment.newInstance(id = cateGroups[position].id)
                }
            }

            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    categoryGroupAdapter.selectedPosition = position
                }
            })
        }
    }
}